package cloud.dwz.controller;

import io.wit.common.annotation.SysLog;
import io.wit.common.utils.PageUtils;
import io.wit.common.utils.Query;
import io.wit.common.utils.R;
import io.wit.common.validator.ValidatorUtils;
import io.wit.config.ConfigFile;
import io.wit.modules.sys.controller.AbstractController;
import io.wit.modules.sys.entity.SysRoleEntity;
import io.wit.modules.sys.service.EmployeeRoleMenuService;
import io.wit.modules.sys.service.EmployeeRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工角色
*/
@RestController
@RequestMapping("/employeeRole")
public class EmployeeRoleController extends AbstractController{

    @Autowired
	private EmployeeRoleService employeeRoleService;

    @Autowired
	private EmployeeRoleMenuService employeeRoleMenuService;

    @Value("${role_value2}")
    private int roleValue2;
	
	/**
	 * 员工角色列表
	*/
	@RequestMapping("/list")
	@RequiresPermissions("employeerole:list")
	public R list(@RequestParam Map<String, Object> params){
		final Query query = new Query(params);
        if(!ConfigFile.superUser.equals(getUserName())){
            final Long subjectId = getSubjectId();
            if(subjectId != null){
                query.put("subjectId",subjectId);
            }else{
                final PageUtils page = new PageUtils(new ArrayList<>(),0,query.getLimit(), query.getPage());
                return R.ok().put("page",page).put("msg","当前账号尚未添加经营主体");
            }
        }
		final List<SysRoleEntity> list = employeeRoleService.queryList(query);
		final int total = employeeRoleService.queryTotal(query);
		final PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
		return R.ok().put("page", page);
	}

	/**
	 * 加载当前账号所拥有的权限菜单
	*/
	@RequestMapping("/select")
	@RequiresPermissions("employeerole:select")
	public R select(){
		final Map<String, Object> map = new HashMap<>();
        if(filterPrivilege(roleValue2)){
            return R.invalidPrivilege();
        }
		final List<HashMap> list = employeeRoleService.queryUserMenu(map);
        return R.queryJson(list);
	}
	
	/**
	 * 勾选'员工角色'对应的菜单数据,当 roleId 为空时说明是添加
	*/
	@RequestMapping("/info")
	@RequiresPermissions("employeerole:info")
	public R info(final Long roleId){
	    if(roleId != null){
            final SysRoleEntity role = employeeRoleService.queryObject(roleId);
            //查询角色对应的菜单
            final List<Long> menuIdList = employeeRoleMenuService.queryMenuIdList(roleId);
            if(role != null && menuIdList.size() > 0){
                role.setMenuIdList(menuIdList);
                return R.ok().put("role",role);
            }else{
                return R.fail("员工角色无任何权限");
            }
        }
        return R.fail("添加员工角色");
	}
	
	/**
	 * 保存员工角色
	*/
	@SysLog("保存员工角色")
	@RequestMapping("/save")
	@RequiresPermissions("employeerole:save")
	public R save(@RequestBody SysRoleEntity role){
        if(filterPrivilege(roleValue2)){
            return R.invalidPrivilege();
        }
		ValidatorUtils.validateEntity(role);
		final int rows = employeeRoleService.save(role);
		return R.execute(rows);
	}
	
	/**
	 * 修改员工角色
	*/
	@SysLog("修改员工角色")
	@RequestMapping("/update")
	@RequiresPermissions("employeerole:update")
	public R update(@RequestBody SysRoleEntity role){
        if(filterPrivilege(roleValue2)){
            return R.invalidPrivilege();
        }
		ValidatorUtils.validateEntity(role);
		employeeRoleService.update(role);
		return R.ok();
	}
	
	/**
	 * 删除角色
	*/
	@SysLog("删除角色")
	@RequestMapping("/delete")
	@RequiresPermissions("employeerole:delete")
	public R delete(@RequestBody Long[] roleIds){
		employeeRoleService.deleteBatch(roleIds);
		return R.ok();
	}
}